package com.CTAtracker.android.Activities;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.DAO.StationDataSource;
import com.CTAtracker.android.Fragments.StationFragment;
import com.CTAtracker.android.Models.Station;

/**
 * The class StationActivity is the activity that deals with a defined station. 
 */
public class StationActivity extends Activity {

	/**
	 * The station to deal with
	 */
	private Station _station;
	/**
	 * The link to the database
	 */
	private StationDataSource _stationDataSource;

	private int _idStation ; 

	private int _idLine;
	/**
	 * This method is called when the activity is created.
	 * It retrieve the data from the Intent, get the station data from 
	 * the database and set up the new fragment
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// set the view
		setContentView(R.layout.mainstation);

		// get the value from the intent
		_idStation = getIntent().getExtras().getInt("idStation");
		_idLine = getIntent().getExtras().getInt("lineId");

		// Inflate Menu
        ActionBar actionbar = getActionBar();
        actionbar.setDisplayShowTitleEnabled(false);
        actionbar.setDisplayShowCustomEnabled(true);
        actionbar.setDisplayShowHomeEnabled(false);
        actionbar.setCustomView(R.layout.actionbar_back);

        ImageView back_button = (ImageView) findViewById(R.id.back_button);
        back_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
		
		// retrieve the data from the database
		_stationDataSource = new StationDataSource(this);
		_stationDataSource.open();
		_station = _stationDataSource.getStationByID(_idStation, _idLine);
		_stationDataSource.close();


		this.replaceContent(new StationFragment());
	}

	@Override
	protected void onResume() {
		super.onResume();
		_station = null;
		// retrieve the data from the database
		_stationDataSource = new StationDataSource(this);
		_stationDataSource.open();
		_station = _stationDataSource.getStationByID(_idStation, _idLine);
		_stationDataSource.close();


		this.replaceContent(new StationFragment());
	}

	/**
	 * This method replace the current fragment
	 * @param frag the new fragment
	 */
	public void replaceContent(Fragment frag) {
		getFragmentManager()
		.beginTransaction()
		.replace(R.id.contentMainStation, frag)
		.commit();
	}

	/**
	 * Getter of Station
	 * @return the station of the activity 
	 */
	public Station getStation(){
		return _station;
	}

}
